package com.luo.service;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luo.dao.TravelItemDao;
import com.luo.entity.QueryPageBean;
import com.luo.pojo.TravelItem;
import com.luo.service.TravelItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Zoro
 * @date 2021年08月23
 */
@Service(interfaceClass = TravelItemService.class)
@Transactional(readOnly = true)
public class TravelItemServiceImpl implements TravelItemService {

    @Autowired
    private TravelItemDao travelItemDao;

    @Override
    @Transactional(readOnly = false)
    public void addTravelItem(TravelItem travelItem) {
        travelItemDao.addTravelItem(travelItem);
    }

    @Override
    public PageInfo<TravelItem> pageSelect(QueryPageBean queryPageBean) {
        //开启分页查询功能（会自动添加limit语句）
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<TravelItem> travelItems = travelItemDao.pageSelect(queryPageBean.getQueryString());
        //返回封装的数据
        return new PageInfo<>(travelItems);
    }

    @Override
    public TravelItem getTravelItemById(Integer id) {
        return travelItemDao.getTravelItemById(id);
    }

    @Transactional(readOnly = false)
    @Override
    public void editTravelItem(TravelItem travelItem) {
        travelItemDao.editTravelItem(travelItem);
    }

    @Transactional(readOnly = false)
    @Override
    public void deleteTravelItem(Integer id) {
        if (travelItemDao.selectItemNum(id) > 0) {
            throw new RuntimeException("存在外键，不能删除");
        } else {
            travelItemDao.deleteTravelItem(id);
        }
    }

    @Override
    public List<TravelItem> selectAll() {
        return travelItemDao.selectAll();
    }
}
